package domain;

import java.util.Collection;
import java.util.HashSet;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.NotBlank;

@Entity
@Access(AccessType.PROPERTY)
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "referenceNumber" }) }, indexes = {@Index(columnList = "availableUnits")} )

public class AvailableItem extends DomainEntity{

	//Constructors
	public AvailableItem(){
		super();
		
		shoppingCartItemUnits = new HashSet<ShoppingCartItemUnit>();
		comments = new HashSet<Comment>();
		wishers = new HashSet<Customer>();
	}
	
	//Attributes

	private String referenceNumber;
	private String name;
	private String description;
	private String state;
	private Money price;
	private int availableUnits;
	
	@NotBlank
	@Pattern(regexp = "^(\\w+-\\w+)+$")
	public String getReferenceNumber() {
		return referenceNumber;
	}
	public void setReferenceNumber(String referenceNumber) {
		this.referenceNumber = referenceNumber;
	}
	
	@NotBlank
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@NotBlank
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	
	@NotBlank
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	
	@NotNull
	@Valid
	public Money getPrice() {
		return price;
	}
	public void setPrice(Money price) {
		this.price = price;
	}
	
	@Min(0)
	public int getAvailableUnits() {
		return availableUnits;
	}
	public void setAvailableUnits(int availableUnits) {
		this.availableUnits = availableUnits;
	}
	
	//Relationships
	
	private Collection<ShoppingCartItemUnit> shoppingCartItemUnits;
	private Collection<Comment> comments;
	private Customer seller;
	private Collection<Customer> wishers;

	@OneToMany(mappedBy = "availableItem")
	public Collection<ShoppingCartItemUnit> getShoppingCartItemUnits() {
		return shoppingCartItemUnits;
	}
	public void setShoppingCartItemUnits(Collection<ShoppingCartItemUnit> shoppingCartItemUnits) {
		this.shoppingCartItemUnits = shoppingCartItemUnits;
	}
	
	@OneToMany(mappedBy = "availableItem")
	public Collection<Comment> getComments() {
		return comments;
	}
	public void setComments(Collection<Comment> comments) {
		this.comments = comments;
	}
	
	@NotNull
	@Valid
	@ManyToOne(optional=false)
	public Customer getSeller() {
		return seller;
	}
	public void setSeller(Customer seller) {
		this.seller = seller;
	}
	
	@ManyToMany(mappedBy = "wishList")
	public Collection<Customer> getWishers() {
		return wishers;
	}
	public void setWishers(Collection<Customer> wishers) {
		this.wishers = wishers;
	}
	
	
}
